﻿namespace Centido.Core.Test
{
    using System.Data.Common;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Testing boolean table columns
    /// </summary>
    [TestClass]
    public class BoolColumnTest : ColumnBaseTest<bool?>
    {
        /// <summary>
        /// Boolean table column
        /// </summary>
        // ReSharper disable UnusedMember.Local
        private BoolColumn Column
        // ReSharper restore UnusedMember.Local
        {
            get { return GenericColumn as BoolColumn; }
        }

        /// <summary>
        /// Get column type
        /// </summary>
        /// <returns></returns>
        protected override ColumnType GetColumnType()
        {
            return ColumnType.Boolean;
        }

        /// <summary>
        /// Get large value
        /// </summary>
        /// <returns></returns>
        protected override bool? GetLargeValue()
        {
            return true;
        }

        /// <summary>
        /// Get small data
        /// </summary>
        /// <returns></returns>
        protected override bool? GetSmallValue()
        {
            return false;
        }

        /// <summary>
        /// Convert db value
        /// </summary>
        /// <param name="dbValue">Db value</param>
        /// <returns></returns>
        protected override bool? ConvertDbValue(object dbValue)
        {
            return DbValue.GetBoolean(dbValue);
        }

        /// <summary>
        /// Inserting valid bool values
        /// </summary>
        public void InsertingValidBoolValues()
        {
            var id = Table.InsertRow(GenericColumn.Value(1));

            Assert.AreEqual(Table.RowCount, 1);
            Assert.AreEqual(GenericColumn.Read(id).GetBoolean(), true);

            id = Table.InsertRow(GenericColumn.Value(0));

            Assert.AreEqual(Table.RowCount, 2);
            Assert.AreEqual(GenericColumn.Read(id).GetBoolean(), false);

            id = Table.InsertRow(GenericColumn.Value(true));

            Assert.AreEqual(Table.RowCount, 3);
            Assert.AreEqual(GenericColumn.Read(id).GetBoolean(), true);

            id = Table.InsertRow(GenericColumn.Value(false));

            Assert.AreEqual(Table.RowCount, 4);
            Assert.AreEqual(GenericColumn.Read(id).GetBoolean(), false);
        }

        /// <summary>
        /// Inserting invalid bool value
        /// </summary>
        [TestMethod]
        public void InsertingInvalidBoolValue()
        {
            try
            {
                Table.InsertRow(GenericColumn.Value(5));
            }
            catch (DbException)
            {
                Assert.AreEqual(Table.RowCount, 0);
                return;
            }

            Assert.Fail();
        }
    }
}